

let recommend = require('../api/migu/recommend');
let search = require('../api/migu/search');
let playlist = require('../api/migu/playlist');
let album = require('../api/migu/album');
let singer = require('../api/migu/singer');
let song = require('../api/migu/song');
let lyric = require('../api/migu/lyric');

export async function top_play_list_MG(f){
  //console.log('migu');
  await recommend.default.playlist({type:2}).then((r) => {
    let result = []
    for (let playlist of r.data.list) {
      result.push(dealmgResult(playlist))
    }
    f(result)
  })
}
function dealmgResult(data){
  let {name:name,id:id,picUrl:imgUrl,creator :{ name: auth}} = data;


  return {
    "name":name,
    "id":id,
    "img":imgUrl,
    "auth":auth,
    "from":'migu'
  }
}


export async function getMgSearch(keywords,pageNo,pageSize,type,f){

  await search.default.index({keyword:keywords,type:type,pageNo:pageNo,pageSize:pageSize}).then((r) => {
    let result;
    //console.log(r);
    if(type === 'song'){
      result = dealSearchSongResult(r.data)
      let count = r.data?.total || 0;
      result = {v:result,count:count}
    }else if(type === 'singer'){
      result = dealSearchSingerResult(r.data)
    }else if(type === 'playlist'){
      result = dealSearchPlayListResult(r.data)
    }else if(type === 'album'){
      result = dealSearchAlbumResult(r.data)
    }


    f(result)
  })
}
function dealSearchAlbumResult(data){
  let als = data.list || []
  return als.map((r) => {

    return {
      "name":r.name,
      "id":r.id,
      "img":r.picUrl,
      "auth":r.artists[0].name,
      "from":'migu'
    }

  })



}
function dealSearchPlayListResult(data){
  let als = data.list || []
  return als.map((r) => {

    return {
      "name":r.name,
      "id":r.id,
      "imgUrl":r.picUrl,
      "auth":r.creator.name,
      "from":'migu'
    }

  })

}
function dealSearchSongResult(data) {
  let tracks = data.list;

  let r = tracks.map((i) => {

    let al = i.album ?i.album.name:""
    let img = i.album ?i.album.picUrl:""
    return{
      'times' : `00 : 00`,
      'name' : i.name,
      'singer' : i.artists[0].name,
      'img' : img,
      'album' : al,
      'id' : i.cid,
      'from' : 'migu'
    }
  })
  return r
}
function dealSearchSingerResult(data) {
  let result = data.list || {artistCount:0,artists:[{}]}
  let map = result.map((r) => {

    return {
      id:r.id,
      name: r.name,
      img: r.picUrl,
      from:'migu'
    }
  });
  return map
}


export async function getMgPlayListDetail(id,f){
  await playlist.default.index({id:id}).then((r) => {
    //console.log(r);
    let result = dealDetailResult(r.data)
    f(result)
  }).catch((r) => {
    return new Promise((resolve, reject) => {
      reject('err');
    })
  })
}
function dealDetailResult(data){

  let {picUrl:img,playCount:playCount,name:name,id:id,desc:description} = data;
  let subscribedCount = ""

  let tags = data.tagLists.map((i) => {
    return i.tagName
  })
  let tracks = data.list;

  let r = tracks.map((i) => {
    let ts = i.duration;

    let min = Math.floor(ts/60);
    let sec = Math.floor(ts - min*60);
    if(min < 10){
      min = '0' + min;
    }
    if(sec < 10){
      sec = '0' + sec;
    }

    let al = i.album ?i.album.name:""
    return{
      'times' : `${min} : ${sec}`,
      'name' : i.name,
      'singer' : i.artists[0].name,
      'album' : al,
      'id' : i.cid,
      from:'migu'
    }
  })


  return {
    user:"",
    tags:tags,
    name:name,
    id:id,
    description:description,
    img:img,
    playCount:playCount,
    subscribedCount:subscribedCount,
    list:r,
    from:'migu'
  }
}
export async function getMgAlbumDetail(id,f){
  await album.default.getAlbum(id).then((r) => {
    //console.log(r)
    let result = dealAlbumDetailResult(r)
    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealAlbumDetailResult(data){

  let tracks = data.songList || [];

  let r = tracks.map((i) => {


    let al = i.album
    let alI = i.albumImgs[0].img
    return{
      'times' : `00 : 00`,
      'name' : i.songName,
      'singer' : i.singer,
      'album' : al,
      'img':alI,
      'id' : i.copyrightId,
      from:'migu'
    }
  })


  return r
}
export async function getMgAlbumBySinger(id,page,perpage,f){
  await singer.default.albums({id:id,pageNo:page}).then((r) => {
    //console.log(r);
    let result = dealAlbumBySinger(r.data);

    f(result);
  }).catch((r) => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealAlbumBySinger(data){
  let tracks = data.list || [];

  let r = tracks.map((i) => {

    return{
      'name' : i.name,
      'singer' : i.artists,
      'img':'http://' + i.picUrl,
      'id' : i.id,
      from:'migu',
      IamAlbum:true
    }
  })
  let result = {};
  result.v = r;
  result.count = data.totalPage;
  //console.log(result);
  return result
}
export async function getMgPlayMusic(id,f){
  await song.default.index({cid:id}).then((r) => {
    //console.log(r)
    let result = dealPlayMusicResult(r.data)
    ////console.log(result)
    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
/**
 * {
 *                     id: "1",
 *                     title: "打上花火",
 *                     info: "DAOKO,米津玄師",
 *                     // resource: require("./resource/DAOKO,米津玄師 - 打上花火.mp3"),
 *                     resource: "",
 *                     time: "04:50",
 *                     img:
 *                         "http://singerimg.kugou.com/uploadpic/softhead/400/20160913/20160913140233132.jpg"
 *                 },
 */
function dealPlayMusicResult(data){
  let resource =  "";
  if(data.flac){
    resource = data.flac;
  }else if(data[320]){
    resource = data[320];
  }else if(data[128]){
    resource = data[128];
  }
  if(resource === ""){
    return -1;
  }
  return {
    img:data.picUrl,
    resource:resource,

  }
}
export async function getMglyric(id,f){
  await lyric.default.index({cid:id}).then((r) => {
    //console.log(r)
    let result = deallyricResult(r)
    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function deallyricResult(data){

  let result = data.data || ''
  while (result.indexOf("\r") !== -1){
    result = result.replace('\r','');
  }

  return result;
}
